/********************************************************************
 *  Proyecto: Sistema modular de comunicacion con un robot movil
 *  Subproyecto: Cliente
 *  Archivo: handler.c
 * 	Version: 0.1
 *
 *  Defines signal handlers.
 * 
 *  Autor: Manuel Angel Abeledo Garcia
 ********************************************************************/

#include <gtk/gtk.h>
#include <glade/glade.h>

#include "general.h"
#include "composer.h"

/* Messages. */
#define ERRORSENDMESSAGE		"Error sending message: "
#define SENDSUCCESFUL			"Message succesfully sent"
#define ERRORRECEIVEMESSAGE		"Error reading message: "
#define DATEMARK				""
#define INITRESULTSVIEW			"-- Initialized at \n"

/* Funcion delete_event
 * Precondiciones:
 * Postcondiciones:
 * Entrada: 
 * Salida: 
 * Proceso: 
 * */
gboolean
delete_event				(GtkWidget* widget, GdkEvent event, gpointer data)
{
	return (FALSE);
}

/* Funcion end_program
 * Precondiciones:
 * Postcondiciones:
 * Entrada: 
 * Salida: 
 * Proceso: 
 * */
void
end_program					(GtkWidget* widget, gpointer data)
{
	gtk_main_quit();
}

/* Funcion on_commandButton_clicked
 * Precondiciones:
 * Postcondiciones:
 * Entrada: 
 * Salida: 
 * Proceso: 
 * */
void
on_commandButton_clicked	(GtkWidget* widget, gpointer data)
{
	GladeXML* uiDefs;
	GtkStatusbar* mainStatusbar;
	const gchar *protocol, *destination, *command;
	gchar* error;
	
	uiDefs = (GladeXML*)data;
	mainStatusbar = GTK_STATUSBAR(glade_xml_get_widget(uiDefs, "mainStatusbar"));
	
	protocol = gtk_entry_get_text(
					GTK_ENTRY(glade_xml_get_widget(uiDefs, "protoEntry"))
				);
	destination = gtk_entry_get_text(
					GTK_ENTRY(glade_xml_get_widget(uiDefs, "destEntry"))
				);
	command = gtk_entry_get_text(
					GTK_ENTRY(glade_xml_get_widget(uiDefs, "commandEntry"))
				);
	
	if (!writeMessage(protocol, destination, command, &error))
	{
		error = g_strconcat(ERRORSENDMESSAGE, error, NULL);
		gtk_statusbar_push(mainStatusbar, 0, error);
	}
	else
	{
		gtk_statusbar_push(mainStatusbar, 0, SENDSUCCESFUL);
	}
}

/* Funcion on_aboutImgMenuItem_activate
 * Precondiciones:
 * Postcondiciones:
 * Entrada: 
 * Salida: 
 * Proceso: 
 * */
void
on_aboutImgMenuItem_activate	(GtkWidget* widget, gpointer data)
{
	GladeXML* uiDefs;
	GtkWindow* mainWindow;
	gchar* authors[] = {AUTHOR, NULL};
	
	uiDefs = (GladeXML*)data;
	mainWindow = GTK_WINDOW(glade_xml_get_widget(uiDefs, "mainWindow"));
	
	gtk_show_about_dialog (mainWindow,
				"logo-icon-name", ICON,
				"program-name", PROGRAM_NAME,
				"version", VERSION,
				"comments", DESCRIPTION,
				"authors", authors,
				"website", WEBSITE_CLIENT,
				NULL);
}

/* Funcion on_quitImgMenuItem_activate
 * Precondiciones:
 * Postcondiciones:
 * Entrada: 
 * Salida: 
 * Proceso: 
 * */
void
on_quitImgMenuItem_activate		(GtkWidget* widget, gpointer data)
{
	GladeXML* uiDefs;
	GtkWindow* mainWindow;
	
	uiDefs = (GladeXML*)data;
	mainWindow = GTK_WINDOW(glade_xml_get_widget(uiDefs, "mainWindow"));
	
	gtk_widget_destroy(GTK_WIDGET(mainWindow));
}

/* Funcion on_saveImgMenuItem_activate
 * Precondiciones:
 * Postcondiciones:
 * Entrada: 
 * Salida: 
 * Proceso: 
 * */
void
on_saveImgMenuItem_activate		(GtkWidget* widget, gpointer data)
{
	GladeXML* uiDefs;
	GtkStatusbar* mainStatusbar;
	
	uiDefs = (GladeXML*)data;
	mainStatusbar = GTK_STATUSBAR(glade_xml_get_widget(uiDefs, "mainStatusbar"));
	
	gtk_statusbar_push(mainStatusbar, 0, "Saved");
}

/* Funcion loadResults
 * Precondiciones:
 * Postcondiciones:
 * Entrada: 
 * Salida: 
 * Proceso: 
 * */
gpointer
loadResults						(gpointer data)
{
	GladeXML* uiDefs;
	gchar *msg, *error;
	GtkTextView* resultsTxt;
	GtkTextBuffer* buffer;
	GtkTextTagTable* tagTable;
	
	uiDefs = (GladeXML*)data;
	resultsTxt = GTK_TEXT_VIEW(glade_xml_get_widget(uiDefs, "resultsTxt"));
	
	tagTable = gtk_text_tag_table_new();
	buffer = gtk_text_buffer_new(tagTable);
	gtk_text_view_set_buffer(resultsTxt, buffer);
	gtk_text_buffer_insert_at_cursor(buffer, g_strconcat(INITRESULTSVIEW, NULL), -1);
	
	while (TRUE)
	{
		msg = NULL;
		
		if (!readMessage(&msg, &error))
		{
			g_warning(g_strconcat(ERRORRECEIVEMESSAGE, error, NULL));
		}
		else
		{
			if (msg != NULL)
			{
				gtk_text_buffer_insert_at_cursor(buffer, g_strconcat(msg, NULL), -1);
			}
		}
	}
	
	return (NULL);	
}
